<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #canvas {
            border: 1px solid;
        }
    </style>
</head>

<body>
    <canvas width="400" height="400" id="canvas">
        请升级你的服务器
    </canvas>
    <script>
        var canvasBox = document.querySelector('#canvas')
        var context = canvasBox.getContext('2d');
        var list = [
            { url: 'https://img0.baidu.com/it/u=2295557875,1506706142&fm=26&fmt=auto&gp=0.jpg'},
            { url: 'https://t7.baidu.com/it/u=2914614332,3549497965&fm=193&f=GIF'},
            { url: 'https://t7.baidu.com/it/u=3725524950,3984082118&fm=193&f=GIF'},
            { url: 'https://t7.baidu.com/it/u=2864756897,2191262990&fm=193&f=GIF'},
            { url: 'https://t7.baidu.com/it/u=3195425099,2044410107&fm=193&f=GIF'},
            ]
            getImage(list,0)
        var p1 = new Promise((resolve,reject)=>{
            var image = new Image()
            image.src = 'https://img0.baidu.com/it/u=2295557875,1506706142&fm=26&fmt=auto&gp=0.jpg'
            image.onload = function () {
                resolve(image)
            }
            image.onerror = function (err) {
                reject(err)
            }
        })
        p1.then((image)=>{
            console.log(image.width,image.height)
            context.drawImage(image, 0, 0, 400, 400);
        }).catch(err=>console.log(err))
        var p2 = new Promise((resolve,reject)=>{
            setTimeout(()=>{
                resolve()
            },200)
        })
        // Promise.all([p1,p2]).then(res=>{

        // })
        // 递归 加载资源
        function getImage(list, i) {
            console.log('list',list)
            var image = new Image()
            image.src = list[i].url
            image.onload = function () {
                list[i].ele = image
                i++
                if (i < list.length) {
                    getImage(list, i)
                }
            }
            image.error = function () {
                console.log('error')
                i++
                if (i < list.length) {
                    getImage(list, i)
                }
            }
        }

    </script>
</body>

</html>